package org.convallaria.system.biz.service;

import java.time.Duration;
import java.util.Map;

/**
 * 安全配置服务接口
 * 
 * @author convallaria
 * @since 1.0.0
 */
public interface SecurityConfigService {

    /**
     * 获取安全配置
     * 
     * @return 安全配置
     */
    SecurityConfig getSecurityConfig();

    /**
     * 更新安全配置
     * 
     * @param config 安全配置
     */
    void updateSecurityConfig(SecurityConfig config);

    /**
     * 获取密码策略配置
     * 
     * @return 密码策略配置
     */
    PasswordPolicyConfig getPasswordPolicyConfig();

    /**
     * 更新密码策略配置
     * 
     * @param config 密码策略配置
     */
    void updatePasswordPolicyConfig(PasswordPolicyConfig config);

    /**
     * 获取登录限制配置
     * 
     * @return 登录限制配置
     */
    LoginLimitConfig getLoginLimitConfig();

    /**
     * 更新登录限制配置
     * 
     * @param config 登录限制配置
     */
    void updateLoginLimitConfig(LoginLimitConfig config);

    /**
     * 获取Token配置
     * 
     * @return Token配置
     */
    TokenConfig getTokenConfig();

    /**
     * 更新Token配置
     * 
     * @param config Token配置
     */
    void updateTokenConfig(TokenConfig config);

    /**
     * 获取配置值
     * 
     * @param key 配置键
     * @return 配置值
     */
    String getConfigValue(String key);

    /**
     * 设置配置值
     * 
     * @param key 配置键
     * @param value 配置值
     */
    void setConfigValue(String key, String value);

    /**
     * 获取所有配置
     * 
     * @return 所有配置
     */
    Map<String, String> getAllConfigs();

    /**
     * 安全配置
     */
    class SecurityConfig {
        private boolean enableCaptcha = true;
        private boolean enableLoginLimit = true;
        private boolean enablePasswordPolicy = true;
        private boolean enableAuditLog = true;
        private boolean enableTokenBlacklist = true;
        private boolean enableDeviceFingerprint = false;
        private boolean enableBehaviorAnalysis = false;
        private Duration sessionTimeout = Duration.ofHours(8);
        private int maxConcurrentSessions = 5;

        // Getters and Setters
        public boolean isEnableCaptcha() { return enableCaptcha; }
        public void setEnableCaptcha(boolean enableCaptcha) { this.enableCaptcha = enableCaptcha; }
        
        public boolean isEnableLoginLimit() { return enableLoginLimit; }
        public void setEnableLoginLimit(boolean enableLoginLimit) { this.enableLoginLimit = enableLoginLimit; }
        
        public boolean isEnablePasswordPolicy() { return enablePasswordPolicy; }
        public void setEnablePasswordPolicy(boolean enablePasswordPolicy) { this.enablePasswordPolicy = enablePasswordPolicy; }
        
        public boolean isEnableAuditLog() { return enableAuditLog; }
        public void setEnableAuditLog(boolean enableAuditLog) { this.enableAuditLog = enableAuditLog; }
        
        public boolean isEnableTokenBlacklist() { return enableTokenBlacklist; }
        public void setEnableTokenBlacklist(boolean enableTokenBlacklist) { this.enableTokenBlacklist = enableTokenBlacklist; }
        
        public boolean isEnableDeviceFingerprint() { return enableDeviceFingerprint; }
        public void setEnableDeviceFingerprint(boolean enableDeviceFingerprint) { this.enableDeviceFingerprint = enableDeviceFingerprint; }
        
        public boolean isEnableBehaviorAnalysis() { return enableBehaviorAnalysis; }
        public void setEnableBehaviorAnalysis(boolean enableBehaviorAnalysis) { this.enableBehaviorAnalysis = enableBehaviorAnalysis; }
        
        public Duration getSessionTimeout() { return sessionTimeout; }
        public void setSessionTimeout(Duration sessionTimeout) { this.sessionTimeout = sessionTimeout; }
        
        public int getMaxConcurrentSessions() { return maxConcurrentSessions; }
        public void setMaxConcurrentSessions(int maxConcurrentSessions) { this.maxConcurrentSessions = maxConcurrentSessions; }
    }

    /**
     * 密码策略配置
     */
    class PasswordPolicyConfig {
        private int minLength = 8;
        private int maxLength = 32;
        private boolean requireLowercase = true;
        private boolean requireUppercase = true;
        private boolean requireDigit = true;
        private boolean requireSpecialChar = true;
        private int historyCount = 5;
        private boolean checkCommonPasswords = true;
        private boolean checkUsername = true;
        private boolean checkPersonalInfo = true;
        private Duration passwordExpireDays = Duration.ofDays(90);
        private int passwordWarnDays = 7;

        // Getters and Setters
        public int getMinLength() { return minLength; }
        public void setMinLength(int minLength) { this.minLength = minLength; }
        
        public int getMaxLength() { return maxLength; }
        public void setMaxLength(int maxLength) { this.maxLength = maxLength; }
        
        public boolean isRequireLowercase() { return requireLowercase; }
        public void setRequireLowercase(boolean requireLowercase) { this.requireLowercase = requireLowercase; }
        
        public boolean isRequireUppercase() { return requireUppercase; }
        public void setRequireUppercase(boolean requireUppercase) { this.requireUppercase = requireUppercase; }
        
        public boolean isRequireDigit() { return requireDigit; }
        public void setRequireDigit(boolean requireDigit) { this.requireDigit = requireDigit; }
        
        public boolean isRequireSpecialChar() { return requireSpecialChar; }
        public void setRequireSpecialChar(boolean requireSpecialChar) { this.requireSpecialChar = requireSpecialChar; }
        
        public int getHistoryCount() { return historyCount; }
        public void setHistoryCount(int historyCount) { this.historyCount = historyCount; }
        
        public boolean isCheckCommonPasswords() { return checkCommonPasswords; }
        public void setCheckCommonPasswords(boolean checkCommonPasswords) { this.checkCommonPasswords = checkCommonPasswords; }
        
        public boolean isCheckUsername() { return checkUsername; }
        public void setCheckUsername(boolean checkUsername) { this.checkUsername = checkUsername; }
        
        public boolean isCheckPersonalInfo() { return checkPersonalInfo; }
        public void setCheckPersonalInfo(boolean checkPersonalInfo) { this.checkPersonalInfo = checkPersonalInfo; }
        
        public Duration getPasswordExpireDays() { return passwordExpireDays; }
        public void setPasswordExpireDays(Duration passwordExpireDays) { this.passwordExpireDays = passwordExpireDays; }
        
        public int getPasswordWarnDays() { return passwordWarnDays; }
        public void setPasswordWarnDays(int passwordWarnDays) { this.passwordWarnDays = passwordWarnDays; }
    }

    /**
     * 登录限制配置
     */
    class LoginLimitConfig {
        private int maxAttempts = 5;
        private Duration lockTime = Duration.ofMinutes(30);
        private Duration attemptWindow = Duration.ofMinutes(15);
        private boolean enableIpLock = true;
        private boolean enableAccountLock = true;
        private int maxIpAttempts = 10;
        private Duration ipLockTime = Duration.ofHours(1);

        // Getters and Setters
        public int getMaxAttempts() { return maxAttempts; }
        public void setMaxAttempts(int maxAttempts) { this.maxAttempts = maxAttempts; }
        
        public Duration getLockTime() { return lockTime; }
        public void setLockTime(Duration lockTime) { this.lockTime = lockTime; }
        
        public Duration getAttemptWindow() { return attemptWindow; }
        public void setAttemptWindow(Duration attemptWindow) { this.attemptWindow = attemptWindow; }
        
        public boolean isEnableIpLock() { return enableIpLock; }
        public void setEnableIpLock(boolean enableIpLock) { this.enableIpLock = enableIpLock; }
        
        public boolean isEnableAccountLock() { return enableAccountLock; }
        public void setEnableAccountLock(boolean enableAccountLock) { this.enableAccountLock = enableAccountLock; }
        
        public int getMaxIpAttempts() { return maxIpAttempts; }
        public void setMaxIpAttempts(int maxIpAttempts) { this.maxIpAttempts = maxIpAttempts; }
        
        public Duration getIpLockTime() { return ipLockTime; }
        public void setIpLockTime(Duration ipLockTime) { this.ipLockTime = ipLockTime; }
    }

    /**
     * Token配置
     */
    class TokenConfig {
        private Duration accessTokenExpire = Duration.ofHours(2);
        private Duration refreshTokenExpire = Duration.ofDays(7);
        private boolean enableRefreshToken = true;
        private boolean enableTokenBlacklist = true;
        private int maxRefreshCount = 5;
        private Duration blacklistExpire = Duration.ofDays(1);

        // Getters and Setters
        public Duration getAccessTokenExpire() { return accessTokenExpire; }
        public void setAccessTokenExpire(Duration accessTokenExpire) { this.accessTokenExpire = accessTokenExpire; }
        
        public Duration getRefreshTokenExpire() { return refreshTokenExpire; }
        public void setRefreshTokenExpire(Duration refreshTokenExpire) { this.refreshTokenExpire = refreshTokenExpire; }
        
        public boolean isEnableRefreshToken() { return enableRefreshToken; }
        public void setEnableRefreshToken(boolean enableRefreshToken) { this.enableRefreshToken = enableRefreshToken; }
        
        public boolean isEnableTokenBlacklist() { return enableTokenBlacklist; }
        public void setEnableTokenBlacklist(boolean enableTokenBlacklist) { this.enableTokenBlacklist = enableTokenBlacklist; }
        
        public int getMaxRefreshCount() { return maxRefreshCount; }
        public void setMaxRefreshCount(int maxRefreshCount) { this.maxRefreshCount = maxRefreshCount; }
        
        public Duration getBlacklistExpire() { return blacklistExpire; }
        public void setBlacklistExpire(Duration blacklistExpire) { this.blacklistExpire = blacklistExpire; }
    }
}
